//处理微信sdk的问题
import Vue from 'vue';
import {configJssdkUrl} from '../../config';
import {post,showToast,getLocalStorage,deleteSessionTokenStr} from './index';
//#ifdef H5
window.wx = require('weixin-js-sdk');
//#endif
//注入配置信息，每个页面都不一样
function config(url, showLoading=true){
	return new Promise((resolve,reject)=>{
		post(configJssdkUrl, {
			url: location.href,
			jsApiList: ['updateAppMessageShareData','updateTimelineShareData','startRecord','stopRecord','onVoiceRecordEnd','previewImage',
				'playVoice','uploadVoice','downloadVoice','pauseVoice','onVoicePlayEnd','chooseWXPay','onMenuShareAppMessage','onMenuShareTimeline']
		}, showLoading)
		.then((data)=>{
			wx.config(data);
			//data.link = url;
			let configObj = getLocalStorage('config');
			let obj = {
				title: configObj.share_title, // 分享标题
				link: deleteSessionTokenStr(url), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				imgUrl: configObj.share_picture, // 分享图标
                success: function () {
                  //showToast('设置成功')
                },
                fail: function (res) {
                  //showToast(JSON.stringify(res))
                },
            };
			shareData(obj);
			resolve(data);
		})
		.catch(()=>{
			reject();
		});
	});
};



//分享接口
function shareData(obj){
	wx.ready(()=>{
		//wx.updateAppMessageShareData(obj); //分享给朋友或者QQ
        //wx.onMenuShareAppMessage(obj);
		//wx.updateTimelineShareData(obj);  //分享到朋友圈或者QQ空间
        //wx.onMenuShareTimeline(obj);
        if(wx.onMenuShareAppMessage){ 
             wx.onMenuShareAppMessage(obj);//1.0 分享到朋友
             wx.onMenuShareTimeline(obj);//1.0分享到朋友圈
         }else{
             wx.updateAppMessageShareData(obj);//1.4 分享到朋友
             wx.updateTimelineShareData(obj);//1.4分享到朋友圈
         }
	});
};


//隐藏分享接口
function hideShareMenu(){
	wx.ready(()=>{
		wx.hideShareMenu({
			menuList: [
				'menuItem:share:appMessage', 'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:weiboApp',
				'menuItem:share:QZone'
			]
		})
	});
}


//调起微信支付
function wx_pay(obj){
	//console.log(obj,111)
	return new Promise((resolve,reject)=>{
		function onBridgeReady(){
			WeixinJSBridge.invoke(
			   'getBrandWCPayRequest', {
					"appId": obj.appId,     //公众号名称，由商户传入     
					"timeStamp": obj.timestamp,         //时间戳，自1970年以来的秒数     
					"nonceStr": obj.nonceStr, //随机串     
					"package": obj.package,     
					"signType":	obj.signType,         //微信签名方式：     
					"paySign": obj.paySign //微信签名 
			    },
			    function(res){
					if(res.err_msg == "get_brand_wcpay_request:ok" ){
						// 使用以上方式判断前端返回,微信团队郑重提示：
						//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
						showToast('支付成功','success');
						resolve();
					}
				},
				function(){
					reject();
				}
			);
		}
		if (typeof WeixinJSBridge == "undefined"){
		   if( document.addEventListener ){
			   document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		   }else if (document.attachEvent){
			   document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
			   document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		   }
		}else{
		   onBridgeReady();
		}
		
		/* wx.ready(()=>{
			wx.chooseWXPay({
				timestamp: obj.timestamp,
				nonceStr: obj.nonceStr,
				package: obj.package,
				signType: obj.signType,
				paySign: obj.paySign,
				success(res){
					showToast('支付成功','success');
					resolve();
				},
				cancel (res) {
					reject();
				},
				fail (res) {
				    showToast('支付失败,请重试');
					reject();
				}
			});
		}); */
	});
}




module.exports = {
	config,
	shareData,
	wx_pay,
	hideShareMenu
}